package com.wzk.exception;

import com.wzk.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler
    public Result ExceptionHandler(BaseException e){
        log.error("出错了！",e);
        return Result.error("服务器异常，请联系管理人员");
    }
    @ExceptionHandler
    public Result DuplicateKeyExceptionHandler(DuplicateKeyException e){
        log.error("出错了！",e);
        String cause = e.getCause().toString();
        String[] split = cause.split(": ");
        String message = split[1].split(" ")[2];
        return Result.error(message+" 冲突了！");
    }

}
